var $myscope, $myhttp;
var uid = localStorage.getItem("gcs_user_id");
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope, $http) {
	$scope.userName = localStorage.getItem("gcs_user_name") + "—";
	$myscope = $scope;
	$myhttp = $http;
	getCostType();
	$("#uid").val(uid);
	$("#start").datetimepicker({
		format: 'yyyy-mm-dd', //格式  如果只有yyyy-mm-dd那就是0000-00-00
		autoclose: true, //选择后是否自动关闭 
		minView: 2, //最精准的时间选择为日期  0-分 1-时 2-日 3-月
		language: 'zh-CN', //中文
		weekStart: 1, //一周从星期几开始
		todayBtn: 1,
		autoclose: 1,
		todayHighlight: 1,
		startView: 2,
		forceParse: 0,
		// daysOfWeekDisabled:"1,2,3", //一周的周几不能选 格式为"1,2,3"  数组格式也行
		todayBtn: true, //在底部是否显示今天
		todayHighlight: false, //今天是否高亮显示
		keyboardNavigation: true, //方向图标改变日期  必须要有img文件夹 里面存放图标
		showMeridian: false, //是否出现 上下午
		initialDate: new Date()
		//startDate: "2017-01-01" //日期开始时间 也可以是new Date()只能选择以后的时间
	}).on("changeDate", function() {
		var start = $("#start").val();
		$("#end").datetimepicker("setStartDate", start);
	});
	$("#end").datetimepicker({
		format: 'yyyy-mm-dd', //格式  如果只有yyyy-mm-dd那就是0000-00-00
		autoclose: true, //选择后是否自动关闭 
		minView: 2, //最精准的时间选择为日期  0-分 1-时 2-日 3-月
		language: 'zh-CN', //中文
		weekStart: 1, //一周从星期几开始
		todayBtn: 1,
		autoclose: 1,
		todayHighlight: 1,
		startView: 2,
		forceParse: 0,
		//daysOfWeekDisabled:"1,2,3", //一周的周几不能选
		todayBtn: true, //在底部是否显示今天
		todayHighlight: false, //今天是否高亮显示
		keyboardNavigation: true, //方向图标改变日期  必须要有img文件夹 里面存放图标
		showMeridian: false //是否出现 上下午
		// startDate: "2017-01-01"  //开始时间  ENdDate 结束时间
	}).on("changeDate", function() {
		var end = $("#end").val();
		$("#start").datetimepicker("setEndDate", end);
	});
	getProject();
	$scope.okProj = function(name, id) {
//		mui('#sheet1').popover('toggle');
		$("#fy_name").val(name)
		$("#fy_id").val(id)
	}
	$scope.okProj2 = function(name, id) {
//		mui('#sheet2').popover('toggle');
alert(id)
		$("#cost").val(name)
		$("#cost_type_id").val(id)
	}
	$scope.deleteProj = function(id) {
		mui.confirm("删除后该项目下的费用都将清空，请谨慎操作", "删除提示", ['取消', '删除'], function(e) {
			if(e.index == 1) {
				$.ajax({
					type: "POST", //方法类型
					dataType: "json", //预期服务器返回的数据类型
					url: deleteProjectUrl, //url
					data: {
						"uid": uid,
						"project_id": id
					},
					success: function(result) {
						if(result.succeed != 0) {
							mui.alert(result.msg);
						} else {
							getProject();
						}
					},
					error: function(result) {
						mui.alert("内部错误！");
					}
				});
			}
		})
	}
	$scope.deleteFY = function(id) {
		mui.confirm("确认删除该费用吗", "删除提示", ['取消', '删除'], function(e) {
			if(e.index == 1) {
				$.ajax({
					type: "POST", //方法类型
					dataType: "json", //预期服务器返回的数据类型
					url: deleteCostUrl, //url
					data: {
						"uid": uid,
						"cost_id": id
					},
					success: function(result) {
						if(result.succeed != 0) {
							mui.alert(result.msg);
						} else {
							getProject();
						}
					},
					error: function(result) {
						mui.alert("内部错误！");
					}
				});
			}
		})
	}
	$scope.addFY = function(id) {
		$("#project_id").val(id);
		$("#addFYModal").modal("show");
	}
	$scope.showImg = function(urls) {
		$("#imgInModalID").attr("src", urls);
		$("#imgModal").modal("show");
	}
	$scope.selectSheet = function() {
		mui('#sheet1').popover('toggle');
	}

	$scope.selectSheet2 = function() {
		mui('#sheet2').popover('toggle');
	}

	$scope.exit = function() {
		localStorage.setItem("isLogin", "0");
		localStorage.removeItem("gcs_user_id");
		localStorage.removeItem("gcs_user_name");
		window.location.href = 'login.html';
	}
	$scope.add = function() {
		$("#addModal").modal("show");
	}
	$scope.getProject = function() {
		getProject();
	}
	$scope.addProjFY = function() {
		var formData = new FormData($("#uploadForm")[0]);
		layer.load(2);
		setTimeout(function() {
			$.ajax({
				async: true,
				cache: false,
				type: "post",
				data: formData,
				url: addCostUrl,
				dataType: 'json',
				timeout: 40000,
				contentType: false, //必须
				processData: false, //必须
				success: function(response) {
					if(response.succeed != 0) {
						mui.alert(response.msg);
					} else {
						$("#addFYModal").modal("hide");
						mui.alert(response.msg);
						$('#uploadForm')[0].reset()
						getProject();
					}
				},
				error: function(arg1, arg2, arg3) {
					mui.alert("网络异常");
				},
				complete: function(XMLRequest, status) {
					layer.closeAll("loading");
					if(status == 'timeout') {
						mui.alert("上传超时，请重新上传");
					}
				}
			});
		}, 1000);
	}
	$scope.addProj = function() {
		$myhttp({
			method: "POST",
			params: {
				"uid": localStorage.getItem("gcs_user_id"),
				"project_name": $("#project_name").val(),
				"remarks": $("#project_desci").val()
			},
			url: addProjectUrl
		}).then(function successCallback(response) {
			mui.alert(response.data.msg)
			$("#addModal").modal("hide");
			if(response.data.succeed != 0) {} else {
				getProject();
			}
		}, function errorCallbcak(response) {
			mui.alert("服务器处理失败")
		});
	}
});
function hh2(){
			$("#cost_type_id").val($("#cost").val())

}
function hh(){
			$("#fy_id").val($("#fy_name").val())

}
function getCostType() {
	$myhttp({
		method: "POST",
		params: {
			"uid": localStorage.getItem("gcs_user_id")
		},
		url: getCostTypeUrl
	}).then(function successCallback(response) {
		if(response.data.succeed != 0) {
			mui.alert(response.data.msg);
		} else {
			$myscope.types = response.data.data;
		}
	}, function errorCallbcak(response) {

	});
}

function getProject() {
	let datas = {
		"uid": localStorage.getItem("gcs_user_id")
	}
	if($("#start").val()) {
		datas["start_time"] = $("#start").val()
	}
	if($("#end").val()) {
		datas["end_time"] = $("#end").val()
	}
	if($("#project").val()) {
		datas["project_name"] = $("#project").val()
	}
	if($("#cost").val() && $("#cost_type_id").val() != 0) {
		datas["cost_type_id"] = $("#cost_type_id").val()
	}
	$myhttp({
		method: "POST",
		params: datas,
		url: getProjectCostUrl
	}).then(function successCallback(response) {
		if(response.data.succeed != 0) {
			$myscope.items = [];
		} else {
			$myscope.items = response.data.data.data;
		}
	}, function errorCallbcak(response) {
		mui.alert("服务器处理失败");
	});
}